Merged
Conversation
…tion
Introduces a single interface that combines deserialization and accessor
management into one scoped operation, replacing the manual three-step pattern
(deserialize → set accessor → finally clear) with a simple using block:
using var _ = propagator.Propagate(metadata);
// context accessible via DI or accessor.GetRequired<T>()
Changes:
- Add IRequestContextPropagator<T> interface (Abstractions/)
- Add RequestContextPropagator<T> implementation (Infrastructure/)
- Register IRequestContextPropagator<> as open-generic singleton in AddTypedRequestContextPropagation()
- Add unit tests covering: context set during scope, cleared after dispose,
cleared even when handler throws, and exception on missing required keys
https://claude.ai/code/session_0123zepdZJiUTyc6XLLezZCv
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces first-class request-context validation (defaulting to DataAnnotations) and a new propagator abstraction to simplify context activation for non-HTTP flows, while refactoring context lifetime management into a reusable scope factory.
Changes:
- Add validation infrastructure (
IRequestContextValidator<T>,RequestContextValidationException, default DataAnnotations validator) and wire it into middleware and registration. - Introduce
IRequestContextPropagator<T>+ implementation to combine deserialization + accessor lifecycle in a singleusingscope for non-HTTP flows. - Refactor accessor lifecycle management into
RequestContextScopeFactory/RequestContextScope, plus cleanup of redundantusingdirectives.
Reviewed changes
Copilot reviewed 27 out of 28 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/TypedRequestContext.UnitTests/RequestContextValidationTests.cs | Adds unit coverage for DataAnnotations validation and scope factory validation behavior. |
| tests/TypedRequestContext.Propagation.UnitTests/PropagationTests.cs | Adds unit coverage for the new IRequestContextPropagator<T> behavior. |
| tests/TypedRequestContext.AspNetCore.IntegrationTests/RequestContextMiddlewareIntegrationTests.cs | Adds integration coverage for HTTP validation failures (400 + structured errors). |
| src/TypedRequestContext/TypedRequestContext.csproj | Adds InternalsVisibleTo for propagation and unit test assemblies. |
| src/TypedRequestContext/Middleware/RequestContextServiceCollectionExtensions.cs | Registers RequestContextScopeFactory and validator services; tracks validator mappings in options. |
| src/TypedRequestContext/Middleware/RequestContextMiddleware.cs | Uses scope factory to manage accessor state; returns structured 400 responses on validation failures. |
| src/TypedRequestContext/Middleware/RequestContextEndpointExtensions.cs | Removes redundant using. |
| src/TypedRequestContext/Infrastructure/RequestContextValidationException.cs | Adds a dedicated exception type for validation failures. |
| src/TypedRequestContext/Infrastructure/RequestContextScopeFactory.cs | Adds centralized context activation + validation + scope creation logic. |
| src/TypedRequestContext/Infrastructure/RequestContextScope.cs | Adds nested-scope support to restore previous context on dispose. |
| src/TypedRequestContext/Infrastructure/RequestContextBuilder.cs | Adds fluent configuration for enabling validation and custom validators. |
| src/TypedRequestContext/Infrastructure/RequestContextAccessor.cs | Removes redundant using. |
| src/TypedRequestContext/Infrastructure/PropertyMapper.cs | Removes redundant using. |
| src/TypedRequestContext/Infrastructure/DataAnnotationsRequestContextValidator.cs | Adds default validator implementation using System.ComponentModel.DataAnnotations. |
| src/TypedRequestContext/Infrastructure/CorrelationContext.cs | Removes redundant using. |
| src/TypedRequestContext/Infrastructure/AttributeBasedRequestContextExtractor.cs | Removes redundant using. |
| src/TypedRequestContext/Abstractions/RequestContextValidationError.cs | Adds a structured validation error record. |
| src/TypedRequestContext/Abstractions/IRequestContextValidator.cs | Adds validator abstraction for typed request contexts. |
| src/TypedRequestContext.Propagation/Infrastructure/RequestContextPropagator.cs | Adds default propagator implementation using deserializer + scope factory. |
| src/TypedRequestContext.Propagation/Infrastructure/PropagationHeadersProvider.cs | Refactors constructor to primary-constructor style; preserves serialization behavior. |
| src/TypedRequestContext.Propagation/Infrastructure/AttributeBasedRequestContextSerializer.cs | Removes redundant using. |
| src/TypedRequestContext.Propagation/Infrastructure/AttributeBasedRequestContextDeserializer.cs | Removes redundant using. |
| src/TypedRequestContext.Propagation/DependencyInjection/TypedRequestContextPropagationServiceCollectionExtensions.cs | Registers IRequestContextPropagator<>. |
| src/TypedRequestContext.Propagation/Abstractions/IRequestContextSerializer.cs | Removes redundant using. |
| src/TypedRequestContext.Propagation/Abstractions/IRequestContextPropagator.cs | Adds new propagator abstraction with overload for scoped validator dependencies. |
| src/TypedRequestContext.Propagation/Abstractions/IRequestContextDeserializer.cs | Removes redundant using. |
| .gitignore | Ignores .claude/settings.local.json. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/TypedRequestContext/Infrastructure/RequestContextValidationException.cs
Outdated
Show resolved
Hide resolved
…nException.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add IRequestContextPropagator for simplified context propagation
Introduce a new interface,
IRequestContextPropagator<T>, to streamline non-HTTP context propagation, replacing a manual three-step process with a more efficient using block. Enhance validation support for typed request contexts using DataAnnotations and improve related classes. Clean up unnecessary using directives and simplify class constructors.